<?php
/**
 * User: zwh
 * Date: 2016-12-22 11:02:54
 */

class OrderPerformance extends Eloquent
{
    protected $table = "order_performance";

    /**
     * @Author:      lyt
     * @DateTime:    2017-02-24 14:23:52
     * @Description: 传入一堆订单检测 有没有包含钣金项目的 返回钣金项目的订单id
     */
    public static function checkOrder($order){
    	$finaly_array=array();
    	$temp_filit=DB::select("SELECT sc3.* FROM service_classification sc3 LEFT JOIN service_classification sc2 on sc3.parent_id=sc2.id LEFT JOIN service_classification sc on sc2.parent_id=sc.id where sc3.`status`=1 and sc.id in(13,14,1553)");
        $filit=array();
        foreach ($temp_filit as $_tf) {
            $filit[]=$_tf->id;
        }
        foreach ($order as $_order) {
            //对该订单进行 钣金喷漆判断 
            $procut_cs=DB::select("SELECT  order_product.product_type,service.classify sc,product_package.classify pc FROM  order_product LEFT JOIN service ON service.id = order_product.product_id LEFT JOIN product_package ON product_package.id = order_product.product_id WHERE   order_product.order_id =? AND order_product.STATUS = 1 AND order_product.product_type IN ('base','non_standard')",array($_order->order_id));
            $has_banji=0;
            foreach ($procut_cs as $_pc) {
                $classify=$_pc->product_type=="base"?$_pc->pc:$_pc->sc;
                if(in_array($classify, $filit)){//如果该订存在钣金喷漆项
                	$finaly_array[]=$_order->order_id;
                }
            }            
        }
        return $finaly_array;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2017-02-24 16:17:01
     * @Description: 删除企业报绩效
     */
    public static function deleteProcut($order_id,$pid){
        $op=OrderPerformance::where('order_id',$order_id)->first();
        if($op){//如果存在绩效记录
            $all_pre=OrderPerformance::where('order_id',$order_id)->get();

            if(date("Y-m",strtotime($op->created_at))!=date("Y-m",time())){//如果不是一个月的 记录负记录
                foreach ($all_pre as $_apre) {
                    $temp=str_replace("[","",$_apre->order_product_id);
                    $temp=str_replace("]","",$temp);
                    $temp=str_replace('"',"",$temp);
                    // $temp=preg_replace("","",$_apre->order_product_id);
                    $array=explode(',',$temp);
                    if(in_array($pid, $array)){//说明 该工序需要记父记录
                        $t_op=new OrderPerformance;
                        $t_op->order_id=$order_id;
                        $t_op->process_id=$_apre->process_id;
                        $t_op->order_product_id='["'.$pid.'"]';
                        $t_op->team_id=$_apre->team_id;
                        $t_op->type=1;//负技校记录
                        $t_op->save();
                        //因为 负绩效员工也是 原来租的员工所以公用员工不新建 员工
                    }
                }  
            }else{//如果是一个月记录删除该项目的 绩效
                foreach ($all_pre as $_apre) {
                    $temp=str_replace("[","",$_apre->order_product_id);
                    $temp=str_replace("]","",$temp);
                    $temp=str_replace('"',"",$temp);
                    // $temp=preg_replace("","",$_apre->order_product_id);
                    $array=explode(',',$temp);
                    foreach( $array as $k=>$v) {
                        if($pid == $v) unset($array[$k]);
                    }
                    $array = array_values($array);
                    if(sizeof($array)>0){
                        $str=implode('","',$array);
                        $_apre->order_product_id='["'.$str.'"]';
                        $_apre->save();
                    }else{//如果 改工序就只记了这一个项目的绩效 删除啊该该绩效和相应绩效 人员信息
                        OrderPerformanceEmployee::where("order_id",$_apre->order_id)->where("process_id",$_apre->process_id)->delete();
                        $_apre->delete();
                    }
                    
                }
            }
            
        }

        
    }
}

